package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Comment;
import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端评论管理控制器
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {
    
    @Autowired
    private CommentMapper commentMapper;
    
    /**
     * 获取评论列表
     */
    @GetMapping("/list")
    public Result<java.util.Map<String, Object>> getCommentList(@RequestParam(required = false) Integer postId,
                                                                @RequestParam(required = false) String keyword,
                                                                @RequestParam(required = false) Integer page,
                                                                @RequestParam(required = false) Integer pageSize) {
        try {
            int safePage = (page == null || page < 1) ? 1 : page;
            int safePageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? 10 : pageSize;
            int offset = (safePage - 1) * safePageSize;
            
            int total = commentMapper.countComments(postId, keyword);
            java.util.List<Comment> list = commentMapper.selectCommentsPaged(postId, keyword, offset, safePageSize);
            
            java.util.Map<String, Object> data = new java.util.HashMap<>();
            data.put("list", list);
            data.put("total", total);
            data.put("page", safePage);
            data.put("pageSize", safePageSize);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取评论列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteComment(@PathVariable Integer id) {
        try {
            // 软删除：修改状态为deleted
            Comment comment = new Comment();
            comment.setId(id);
            comment.setStatus("deleted");
            int result = commentMapper.updateComment(comment);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除评论失败");
            }
        } catch (Exception e) {
            return Result.error("删除评论失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除评论
     */
    @PostMapping("/batchDelete")
    public Result<Void> batchDeleteComments(@RequestBody List<Integer> ids) {
        try {
            int successCount = 0;
            for (Integer id : ids) {
                Comment comment = new Comment();
                comment.setId(id);
                comment.setStatus("deleted");
                int result = commentMapper.updateComment(comment);
                if (result > 0) {
                    successCount++;
                }
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
}

